package com.test.demo60;


public class Wife {

    private static final Byte[] bytes = new Byte[0];

    public static void main(String[] args) {
        // 取得线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (bytes) {
                    while (true) {
                        Integer money = MAN.getMoney();
                        System.out.println("老婆取钱" + (money)+"元");
                        if (money==0) {
                            try {
                                System.out.println("钱不足,剩余:"+money);
                                bytes.notifyAll();
                                System.out.println("老公存钱了");
                                Thread.sleep(5000);
                                bytes.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        }).start();
        // 放的线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (bytes) {
                    while (true) {
                        MAN.add();

                        bytes.notifyAll();
                        try {
                            bytes.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }).start();
    }
}
class MAN {
    private static int count = 0;

    static {
        add();
    }

    public static Integer getMoney() {
        return count-=1000;
    }

    public static void add() {
        count += 10000;
    }
}
